package com.jackrain.nea.oms.services;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.aliyun.openservices.ons.api.impl.util.MsgConvertUtil;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.UUID;

/**
 * Description:
 * Created with IntelliJ IDEA.
 * User: cs
 * Date: 2017/3/28
 * Time: 10:05
 */

@Component
@Slf4j
public class SendMqServies {

    @Autowired
    private ProducerBean producerBean;

    /**
     * 发送消息
     *
     * @param dataarry
     * @param ctag
     * @throws Exception
     */
    public SendResult sendMqinfo(JSONArray dataarry, String topic, String ctag, ValueHolder valueHolder) throws Exception {


        Message msg = null;
        //  Expr 就是 tag
        msg = new Message(topic, ctag, MsgConvertUtil
                .objectSerialize(dataarry));
        String str = UUID.randomUUID().toString();
        msg.setKey(str);

        //获取本身bean
        SendResult sendResult = ApplicationContextHandle.getBean(SendMqServies.class).send(msg, valueHolder);

        if (sendResult != null) {
            log.debug("send success: " + sendResult.getMessageId() + topic);
        }

        return sendResult;

    }


    /**
     * 发送消息
     *
     * @param dataobject
     * @param topic
     * @param ctag
     * @throws Exception
     */
    public SendResult sendMqinfoObj(JSONObject dataobject, String topic, String ctag, ValueHolder valueHolder) throws Exception {

        Message msg = null;
        msg = new Message(topic, ctag, MsgConvertUtil
                .objectSerialize(dataobject));
        String str = UUID.randomUUID().toString();
        msg.setKey(str);

        //获取本身bean发送
        SendResult sendResult = ApplicationContextHandle.getBean(SendMqServies.class).send(msg, valueHolder);

        if (sendResult != null) {
            log.debug("send success: " + sendResult.getMessageId() + topic);
        }

        return sendResult;
    }

    /**
     * 发送消息 无切面
     *
     * @param dataobject
     * @param topic
     * @param ctag
     * @return
     * @throws Exception
     */
    public SendResult sendMqObj(JSONObject dataobject, String topic, String ctag) throws Exception {

        Message msg = null;
        msg = new Message(topic, ctag, MsgConvertUtil.objectSerialize(dataobject));
        String str = UUID.randomUUID().toString();
        msg.setKey(str);

        //获取本身bean发送
        SendResult sendResult = producerBean.send(msg);

        if (sendResult != null) {
            log.debug("send success: " + sendResult.getMessageId() + topic);
        }


        return sendResult;
    }

    public String sendMqObj2(JSONObject dataobject, String topic, String ctag, String sellerNick) throws Exception {

        PropertiesConf propertiesConf = ApplicationContextHandle.getBean(PropertiesConf.class);
        String jdLogRetTopic = propertiesConf.getProperty("ons.jdLogRetTopic", "");
        String jdLogErrTopic = propertiesConf.getProperty("ons.jdLogErrTopic", "");
        Message msg = null;
        SendResult sendResult = null;
        try {
            msg = new Message(topic, ctag, MsgConvertUtil
                    .objectSerialize(dataobject));
            String str = UUID.randomUUID().toString();
            msg.setKey(str);
            //获取本身bean发送
            sendResult = producerBean.send(msg);

            if (sendResult != null) {
                log.debug("send success: " + sendResult.getMessageId() + topic);
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject errLogObject = new JSONObject();
            errLogObject.put("flag", 1);
            errLogObject.put("errmsg", "线上消息发送失败");
            errLogObject.put("topic", topic);
            errLogObject.put("tag", ctag);
            errLogObject.put("sellernick", sellerNick);
            sendMqObj(errLogObject, jdLogErrTopic, ctag);
            msg = new Message(topic, ctag, MsgConvertUtil
                    .objectSerialize(dataobject));
            String str = UUID.randomUUID().toString();
            msg.setKey(str);
            //获取本身bean发送
            sendResult = producerBean.send(msg);

            if (sendResult != null) {
                log.debug("send success:" + sendResult.getMessageId() + topic);
            }
        }

        JSONObject recordObject = new JSONObject();
        recordObject.put("flag", 1);
        recordObject.put("sellernick", sellerNick);
        recordObject.put("msgsize", 1);
        recordObject.put("msgkey", msg.getKey());
        recordObject.put("topic", topic);
        recordObject.put("tag", ctag);
        recordObject.put("sendtime", System.currentTimeMillis());
        //消息发送成功，将消息信息发送到监控中心
        sendMqObj(recordObject, jdLogRetTopic, ctag);
        return msg.getKey();
    }

    public String sendMqArr(JSONArray dataArray, String topic, String ctag, String sellerNick) throws Exception {

        PropertiesConf propertiesConf = ApplicationContextHandle.getBean(PropertiesConf.class);
        String jdLogRetTopic = propertiesConf.getProperty("ons.jdLogRetTopic", "");
        String jdLogErrTopic = propertiesConf.getProperty("ons.jdLogErrTopic", "");
        Message msg = null;
        SendResult sendResult = null;
        try {
            msg = new Message(topic, ctag, MsgConvertUtil
                    .objectSerialize(dataArray));
            String str = UUID.randomUUID().toString();
            msg.setKey(str);
            //获取本身bean发送
            sendResult = producerBean.send(msg);

            if (sendResult != null) {
                log.debug("send success:" + sendResult.getMessageId() + topic);
            }
        } catch (IOException e) {
            e.printStackTrace();
            JSONObject errLogObject = new JSONObject();
            errLogObject.put("flag", 1);
            errLogObject.put("errmsg", "消息发送失败。");
            errLogObject.put("topic", topic);
            errLogObject.put("tag", ctag);
            errLogObject.put("sellernick", sellerNick);
            sendMqObj(errLogObject, jdLogErrTopic, ctag);
            msg = new Message(topic, ctag, MsgConvertUtil
                    .objectSerialize(dataArray));
            String str = UUID.randomUUID().toString();
            msg.setKey(str);
            //获取本身bean发送
            sendResult = producerBean.send(msg);

            if (sendResult != null) {
                log.debug("send success: " + sendResult.getMessageId() + topic);
            }
        }

        JSONObject recordObject = new JSONObject();
        recordObject.put("flag", 1);
        recordObject.put("sellerNick", sellerNick);
        recordObject.put("msgSize", dataArray.size());
        recordObject.put("msgKey", msg.getKey());
        recordObject.put("topic", topic);
        recordObject.put("tag", ctag);
        recordObject.put("sendtime", System.currentTimeMillis());
        //消息发送成功，将消息信息发送到监控中心
        sendMqObj(recordObject, jdLogRetTopic, ctag);
        return msg.getKey();
    }

    public SendResult sendMqArr2(JSONArray dataArray, String topic, String ctag) throws Exception {
        Message msg = new Message(topic, ctag, MsgConvertUtil
                .objectSerialize(dataArray));
        String str = UUID.randomUUID().toString();
        msg.setKey(str);

        //获取本身bean发送
        SendResult sendResult = producerBean.send(msg);

        if (sendResult != null) {
            log.debug("send success: " + sendResult.getMessageId() + topic);
        }

        return sendResult;
    }

    public SendResult sendMqinfoLogObj(JSONObject dataobject, String topic, String ctag) throws Exception {


        Message msg = new Message(topic, ctag, MsgConvertUtil
                .objectSerialize(dataobject));
        String str = UUID.randomUUID().toString();
        msg.setKey(str);

        //获取本身bean发送
        SendResult sendResult = producerBean.send(msg);

        if (sendResult != null) {
            log.debug("send success: " + sendResult.getMessageId() + topic);
        }

        return sendResult;
    }

    public SendResult sendMqRetLogObj(JSONObject dataobject, String topic, String ctag, String sellerNick, int msgSize) throws Exception {
        PropertiesConf propertiesConf = ApplicationContextHandle.getBean(PropertiesConf.class);
        String jdLogRetTopic = propertiesConf.getProperty("ons.jdLogRetTopic", "");
        String jdLogErrTopic = propertiesConf.getProperty("ons.jdLogErrTopic", "");
        Message msg = null;
        SendResult sendResult = null;
        try {
            msg = new Message(topic, ctag, MsgConvertUtil
                    .objectSerialize(dataobject));
            String str = UUID.randomUUID().toString();
            msg.setKey(str);
            //获取本身bean发送
            sendResult = producerBean.send(msg);

            if (sendResult != null) {
                log.debug("send success: " + sendResult.getMessageId() + topic);
            }
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject errLogObject = new JSONObject();
            errLogObject.put("flag", 1);
            errLogObject.put("errmsg", "线上消息发送失败");
            errLogObject.put("topic", topic);
            errLogObject.put("tag", ctag);
            errLogObject.put("sellernick", sellerNick);
            sendMqObj(errLogObject, jdLogErrTopic, ctag);
            msg = new Message(topic, ctag, MsgConvertUtil
                    .objectSerialize(dataobject));
            String str = UUID.randomUUID().toString();
            msg.setKey(str);
            //获取本身bean发送
            sendResult = producerBean.send(msg);

            if (sendResult != null) {
                log.debug("send success:" + sendResult.getMessageId() + topic);
            }
        }

        JSONObject recordObject = new JSONObject();
        recordObject.put("flag", 1);
        recordObject.put("sellernick", sellerNick);
        recordObject.put("msgsize", msgSize);
        recordObject.put("msgkey", msg.getKey());
        recordObject.put("topic", topic);
        recordObject.put("tag", ctag);
        recordObject.put("sendtime", System.currentTimeMillis());
        //消息发送成功，将消息信息发送到监控中心
        sendMqObj(recordObject, jdLogRetTopic, ctag);
        return sendResult;
    }


    public SendResult sendMqRetLogArr(JSONArray dataArray, String topic, String ctag, String sellerNick) throws Exception {
        PropertiesConf propertiesConf = ApplicationContextHandle.getBean(PropertiesConf.class);
        String jdLogRetTopic = propertiesConf.getProperty("ons.jdLogRetTopic", "");
        String jdLogErrTopic = propertiesConf.getProperty("ons.jdLogErrTopic", "");
        Message msg = null;
        SendResult sendResult = null;
        try {
            msg = new Message(topic, ctag, MsgConvertUtil
                    .objectSerialize(dataArray));
            String str = UUID.randomUUID().toString();
            msg.setKey(str);
            //获取本身bean发送
            sendResult = producerBean.send(msg);

            if (sendResult != null) {
                log.debug("send success:" + sendResult.getMessageId() + topic);
            }
        } catch (IOException e) {
            e.printStackTrace();
            JSONObject errLogObject = new JSONObject();
            errLogObject.put("flag", 1);
            errLogObject.put("errmsg", "消息发送失败。");
            errLogObject.put("topic", topic);
            errLogObject.put("tag", ctag);
            errLogObject.put("sellernick", sellerNick);
            sendMqObj(errLogObject, jdLogErrTopic, ctag);
            msg = new Message(topic, ctag, MsgConvertUtil
                    .objectSerialize(dataArray));
            String str = UUID.randomUUID().toString();
            msg.setKey(str);
            //获取本身bean发送
            sendResult = producerBean.send(msg);

            if (sendResult != null) {
                log.debug("send success: " + sendResult.getMessageId() + topic);
            }
        }

        JSONObject recordObject = new JSONObject();
        recordObject.put("flag", 1);
        recordObject.put("sellerNick", sellerNick);
        recordObject.put("msgSize", dataArray.size());
        recordObject.put("msgKey", msg.getKey());
        recordObject.put("topic", topic);
        recordObject.put("tag", ctag);
        recordObject.put("sendtime", System.currentTimeMillis());
        //消息发送成功，将消息信息发送到监控中心
        sendMqObj(recordObject, jdLogRetTopic, ctag);
        return sendResult;
    }

    /**
     * 发送消息 无切面
     *
     * @param dataArray
     * @param topic
     * @param ctag
     * @return
     * @throws Exception
     */
    public SendResult sendMqinfoLogObj(JSONArray dataArray, String topic, String ctag) throws Exception {

        Message msg = new Message(topic, ctag, MsgConvertUtil
                .objectSerialize(dataArray));
        String str = UUID.randomUUID().toString();
        msg.setKey(str);

        //获取本身bean发送
        SendResult sendResult = producerBean.send(msg);

        if (sendResult != null) {
            log.debug("send success: " + sendResult.getMessageId() + topic);
        }

        return sendResult;
    }


    public SendResult send(Message msg, ValueHolder valueHolder) {

        log.debug("sended");

        return producerBean.send(msg);

    }

    public SendResult send(Message msg) {
        log.debug("sended");

        return producerBean.send(msg);
    }


}